package org.dromara.admin.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 打印类型枚举: 针式打印机(DOT_MATRIX)、小票机(RECEIPT)、A4打印机(A4)
 */
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT) // ✅ 序列化为对象形式（code + info）
public enum PrintTypeEnum {

    DOT_MATRIX("DOT_MATRIX", "针式打印机"),
    RECEIPT("RECEIPT", "小票机"),
    A4("A4", "A4打印机");

    @EnumValue // ✅ MyBatis Plus 存储时用 code
    @JsonValue // ✅ 返回给前端时显示 code
    private final String code;
    private final String info;

    PrintTypeEnum(String code, String info) {
        this.code = code;
        this.info = info;
    }

    @JsonCreator
    public static PrintTypeEnum fromCode(String code) {
        if (code == null) return null;
        for (PrintTypeEnum e : values()) {
            if (e.code.equalsIgnoreCase(code)) {
                return e;
            }
        }
        return null; // ✅ 不匹配时返回 null
    }

    @JsonValue
    public Map<String, String> toJson() {
        Map<String, String> map = new HashMap<>();
        map.put("code", this.code);
        map.put("info", this.info);
        return map;
    }
}
